package androidx.appcompat.view.menu;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Parcelable;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.core.view.ViewCompat;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import o.ah$j;
import o.ah.e;
import o.bd;
import o.bm;
import o.bn;
import o.bn.d;
import o.bv;
import o.cn;
import o.cq;
import o.hj;

public final class CascadingMenuPopup extends bm implements OnKeyListener, OnDismissListener {
    private static final int ʼ = ah$j.ॱॱ;
    boolean ʻ;
    private boolean ʻॱ;
    private boolean ʼॱ;
    private final int ʽ;
    private int ʽॱ;
    private boolean ʾ;
    private int ʿ;
    private boolean ˈ;
    View ˊ;
    private OnDismissListener ˊˋ;
    private final List<bd> ˊॱ = new ArrayList();
    private d ˊᐝ;
    final Handler ˋ;
    private final cq ˋॱ = new cq(this) {
        final /* synthetic */ CascadingMenuPopup ˋ;

        {
            this.ˋ = r1;
        }

        public void ˊ(@NonNull bd bdVar, @NonNull MenuItem menuItem) {
            this.ˋ.ˋ.removeCallbacksAndMessages(bdVar);
        }

        public void ˏ(@NonNull final bd bdVar, @NonNull final MenuItem menuItem) {
            int i;
            this.ˋ.ˋ.removeCallbacksAndMessages(null);
            int size = this.ˋ.ˎ.size();
            for (int i2 = 0; i2 < size; i2++) {
                if (bdVar == ((a) this.ˋ.ˎ.get(i2)).ˏ) {
                    i = i2;
                    break;
                }
            }
            i = -1;
            if (i != -1) {
                a aVar;
                i++;
                if (i < this.ˋ.ˎ.size()) {
                    aVar = (a) this.ˋ.ˎ.get(i);
                } else {
                    aVar = null;
                }
                this.ˋ.ˋ.postAtTime(new Runnable(this) {
                    final /* synthetic */ AnonymousClass3 ॱ;

                    public void run() {
                        if (aVar != null) {
                            this.ॱ.ˋ.ʻ = true;
                            aVar.ˏ.ˏ(false);
                            this.ॱ.ˋ.ʻ = false;
                        }
                        if (menuItem.isEnabled() && menuItem.hasSubMenu()) {
                            bdVar.ˎ(menuItem, 4);
                        }
                    }
                }, bdVar, SystemClock.uptimeMillis() + 200);
            }
        }
    };
    final List<a> ˎ = new ArrayList();
    ViewTreeObserver ˏ;
    private final OnAttachStateChangeListener ˏॱ = new OnAttachStateChangeListener(this) {
        final /* synthetic */ CascadingMenuPopup ˎ;

        {
            this.ˎ = r1;
        }

        public void onViewAttachedToWindow(View view) {
        }

        public void onViewDetachedFromWindow(View view) {
            if (this.ˎ.ˏ != null) {
                if (!this.ˎ.ˏ.isAlive()) {
                    this.ˎ.ˏ = view.getViewTreeObserver();
                }
                this.ˎ.ˏ.removeGlobalOnLayoutListener(this.ˎ.ॱ);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    };
    private final int ͺ;
    final OnGlobalLayoutListener ॱ = new OnGlobalLayoutListener(this) {
        final /* synthetic */ CascadingMenuPopup ˊ;

        {
            this.ˊ = r1;
        }

        public void onGlobalLayout() {
            if (this.ˊ.ˎ() && this.ˊ.ˎ.size() > 0 && !((a) this.ˊ.ˎ.get(0)).ˎ.ॱॱ()) {
                View view = this.ˊ.ˊ;
                if (view == null || !view.isShown()) {
                    this.ˊ.ˋ();
                    return;
                }
                for (a aVar : this.ˊ.ˎ) {
                    aVar.ˎ.ˊ();
                }
            }
        }
    };
    private final boolean ॱˊ;
    private int ॱˋ = 0;
    private View ॱˎ;
    private final int ॱॱ;
    private int ॱᐝ = 0;
    private final Context ᐝ;
    private int ᐝॱ;

    @Retention(RetentionPolicy.SOURCE)
    public @interface HorizPosition {
    }

    static class a {
        public final int ˋ;
        public final cn ˎ;
        public final bd ˏ;

        public a(@NonNull cn cnVar, @NonNull bd bdVar, int i) {
            this.ˎ = cnVar;
            this.ˏ = bdVar;
            this.ˋ = i;
        }

        public ListView ˋ() {
            return this.ˎ.ᐝ();
        }
    }

    public CascadingMenuPopup(@NonNull Context context, @NonNull View view, @AttrRes int i, @StyleRes int i2, boolean z) {
        this.ᐝ = context;
        this.ॱˎ = view;
        this.ʽ = i;
        this.ͺ = i2;
        this.ॱˊ = z;
        this.ʾ = false;
        this.ᐝॱ = ˋॱ();
        Resources resources = context.getResources();
        this.ॱॱ = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(e.ˋ));
        this.ˋ = new Handler();
    }

    public void ˎ(boolean z) {
        this.ʾ = z;
    }

    private cn ʼ() {
        cn cnVar = new cn(this.ᐝ, null, this.ʽ, this.ͺ);
        cnVar.ˎ(this.ˋॱ);
        cnVar.ˊ((OnItemClickListener) this);
        cnVar.ॱ((OnDismissListener) this);
        cnVar.ˏ(this.ॱˎ);
        cnVar.ॱ(this.ॱᐝ);
        cnVar.ˎ(true);
        cnVar.ʻ(2);
        return cnVar;
    }

    public void ˊ() {
        if (!ˎ()) {
            for (bd ˋ : this.ˊॱ) {
                ˋ(ˋ);
            }
            this.ˊॱ.clear();
            this.ˊ = this.ॱˎ;
            if (this.ˊ != null) {
                Object obj = this.ˏ == null ? 1 : null;
                this.ˏ = this.ˊ.getViewTreeObserver();
                if (obj != null) {
                    this.ˏ.addOnGlobalLayoutListener(this.ॱ);
                }
                this.ˊ.addOnAttachStateChangeListener(this.ˏॱ);
            }
        }
    }

    public void ˋ() {
        int size = this.ˎ.size();
        if (size > 0) {
            a[] aVarArr = (a[]) this.ˎ.toArray(new a[size]);
            for (size--; size >= 0; size--) {
                a aVar = aVarArr[size];
                if (aVar.ˎ.ˎ()) {
                    aVar.ˎ.ˋ();
                }
            }
        }
    }

    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() != 1 || i != 82) {
            return false;
        }
        ˋ();
        return true;
    }

    private int ˋॱ() {
        if (ViewCompat.ʼ(this.ॱˎ) == 1) {
            return 0;
        }
        return 1;
    }

    private int ˋ(int i) {
        ListView ˋ = ((a) this.ˎ.get(this.ˎ.size() - 1)).ˋ();
        int[] iArr = new int[2];
        ˋ.getLocationOnScreen(iArr);
        Rect rect = new Rect();
        this.ˊ.getWindowVisibleDisplayFrame(rect);
        if (this.ᐝॱ == 1) {
            if ((ˋ.getWidth() + iArr[0]) + i > rect.right) {
                return 0;
            }
            return 1;
        } else if (iArr[0] - i < 0) {
            return 1;
        } else {
            return 0;
        }
    }

    public void ˊ(bd bdVar) {
        bdVar.ˏ(this, this.ᐝ);
        if (ˎ()) {
            ˋ(bdVar);
        } else {
            this.ˊॱ.add(bdVar);
        }
    }

    private void ˋ(@NonNull bd bdVar) {
        View ˏ;
        LayoutInflater from = LayoutInflater.from(this.ᐝ);
        Object menuAdapter = new MenuAdapter(bdVar, from, this.ॱˊ, ʼ);
        if (!ˎ() && this.ʾ) {
            menuAdapter.setForceShowIcon(true);
        } else if (ˎ()) {
            menuAdapter.setForceShowIcon(bm.ˏ(bdVar));
        }
        int ˏ2 = bm.ˏ(menuAdapter, null, this.ᐝ, this.ॱॱ);
        cn ʼ = ʼ();
        ʼ.ˏ((ListAdapter) menuAdapter);
        ʼ.ʼ(ˏ2);
        ʼ.ॱ(this.ॱᐝ);
        if (this.ˎ.size() > 0) {
            a aVar = (a) this.ˎ.get(this.ˎ.size() - 1);
            ˏ = ˏ(aVar, bdVar);
            a aVar2 = aVar;
        } else {
            ˏ = null;
            Object obj = null;
        }
        if (ˏ != null) {
            boolean z;
            int i;
            int i2;
            ʼ.ˋ(false);
            ʼ.ॱ(null);
            int ˋ = ˋ(ˏ2);
            if (ˋ == 1) {
                z = true;
            } else {
                z = false;
            }
            this.ᐝॱ = ˋ;
            if (VERSION.SDK_INT >= 26) {
                ʼ.ˏ(ˏ);
                ˋ = 0;
                i = 0;
            } else {
                int[] iArr = new int[2];
                this.ॱˎ.getLocationOnScreen(iArr);
                int[] iArr2 = new int[2];
                ˏ.getLocationOnScreen(iArr2);
                if ((this.ॱᐝ & 7) == 5) {
                    iArr[0] = iArr[0] + this.ॱˎ.getWidth();
                    iArr2[0] = iArr2[0] + ˏ.getWidth();
                }
                i = iArr2[0] - iArr[0];
                ˋ = iArr2[1] - iArr[1];
            }
            if ((this.ॱᐝ & 5) == 5) {
                if (z) {
                    i2 = i + ˏ2;
                } else {
                    i2 = i - ˏ.getWidth();
                }
            } else if (z) {
                i2 = ˏ.getWidth() + i;
            } else {
                i2 = i - ˏ2;
            }
            ʼ.ˎ(i2);
            ʼ.ˏ(true);
            ʼ.ˏ(ˋ);
        } else {
            if (this.ʻॱ) {
                ʼ.ˎ(this.ʿ);
            }
            if (this.ʼॱ) {
                ʼ.ˏ(this.ʽॱ);
            }
            ʼ.ˏ(ॱॱ());
        }
        this.ˎ.add(new a(ʼ, bdVar, this.ᐝॱ));
        ʼ.ˊ();
        ViewGroup ᐝ = ʼ.ᐝ();
        ᐝ.setOnKeyListener(this);
        if (aVar2 == null && this.ˈ && bdVar.ˏॱ() != null) {
            FrameLayout frameLayout = (FrameLayout) from.inflate(ah$j.ˏॱ, ᐝ, false);
            TextView textView = (TextView) frameLayout.findViewById(16908310);
            frameLayout.setEnabled(false);
            textView.setText(bdVar.ˏॱ());
            ᐝ.addHeaderView(frameLayout, null, false);
            ʼ.ˊ();
        }
    }

    private MenuItem ˋ(@NonNull bd bdVar, @NonNull bd bdVar2) {
        int size = bdVar.size();
        for (int i = 0; i < size; i++) {
            MenuItem item = bdVar.getItem(i);
            if (item.hasSubMenu() && bdVar2 == item.getSubMenu()) {
                return item;
            }
        }
        return null;
    }

    @Nullable
    private View ˏ(@NonNull a aVar, @NonNull bd bdVar) {
        int i = 0;
        MenuItem ˋ = ˋ(aVar.ˏ, bdVar);
        if (ˋ == null) {
            return null;
        }
        int headersCount;
        MenuAdapter menuAdapter;
        int i2;
        ListView ˋ2 = aVar.ˋ();
        ListAdapter adapter = ˋ2.getAdapter();
        if (adapter instanceof HeaderViewListAdapter) {
            HeaderViewListAdapter headerViewListAdapter = (HeaderViewListAdapter) adapter;
            headersCount = headerViewListAdapter.getHeadersCount();
            menuAdapter = (MenuAdapter) headerViewListAdapter.getWrappedAdapter();
        } else {
            menuAdapter = (MenuAdapter) adapter;
            headersCount = 0;
        }
        int count = menuAdapter.getCount();
        while (i < count) {
            if (ˋ == menuAdapter.getItem(i)) {
                i2 = i;
                break;
            }
            i++;
        }
        i2 = -1;
        if (i2 == -1) {
            return null;
        }
        i2 = (i2 + headersCount) - ˋ2.getFirstVisiblePosition();
        if (i2 < 0 || i2 >= ˋ2.getChildCount()) {
            return null;
        }
        return ˋ2.getChildAt(i2);
    }

    public boolean ˎ() {
        return this.ˎ.size() > 0 && ((a) this.ˎ.get(0)).ˎ.ˎ();
    }

    public void onDismiss() {
        a aVar;
        int size = this.ˎ.size();
        for (int i = 0; i < size; i++) {
            aVar = (a) this.ˎ.get(i);
            if (!aVar.ˎ.ˎ()) {
                break;
            }
        }
        aVar = null;
        if (aVar != null) {
            aVar.ˏ.ˏ(false);
        }
    }

    public void ˊ(boolean z) {
        for (a ˋ : this.ˎ) {
            bm.ॱ(ˋ.ˋ().getAdapter()).notifyDataSetChanged();
        }
    }

    public void ˎ(d dVar) {
        this.ˊᐝ = dVar;
    }

    public boolean ˎ(bv bvVar) {
        for (a aVar : this.ˎ) {
            if (bvVar == aVar.ˏ) {
                aVar.ˋ().requestFocus();
                return true;
            }
        }
        if (!bvVar.hasVisibleItems()) {
            return false;
        }
        ˊ((bd) bvVar);
        if (this.ˊᐝ != null) {
            this.ˊᐝ.ˎ(bvVar);
        }
        return true;
    }

    private int ˎ(@NonNull bd bdVar) {
        int size = this.ˎ.size();
        for (int i = 0; i < size; i++) {
            if (bdVar == ((a) this.ˎ.get(i)).ˏ) {
                return i;
            }
        }
        return -1;
    }

    public void ˏ(bd bdVar, boolean z) {
        int ˎ = ˎ(bdVar);
        if (ˎ >= 0) {
            int i = ˎ + 1;
            if (i < this.ˎ.size()) {
                ((a) this.ˎ.get(i)).ˏ.ˏ(false);
            }
            a aVar = (a) this.ˎ.remove(ˎ);
            aVar.ˏ.ˏ((bn) this);
            if (this.ʻ) {
                aVar.ˎ.ˏ(null);
                aVar.ˎ.ˊ(0);
            }
            aVar.ˎ.ˋ();
            ˎ = this.ˎ.size();
            if (ˎ > 0) {
                this.ᐝॱ = ((a) this.ˎ.get(ˎ - 1)).ˋ;
            } else {
                this.ᐝॱ = ˋॱ();
            }
            if (ˎ == 0) {
                ˋ();
                if (this.ˊᐝ != null) {
                    this.ˊᐝ.ॱ(bdVar, true);
                }
                if (this.ˏ != null) {
                    if (this.ˏ.isAlive()) {
                        this.ˏ.removeGlobalOnLayoutListener(this.ॱ);
                    }
                    this.ˏ = null;
                }
                this.ˊ.removeOnAttachStateChangeListener(this.ˏॱ);
                this.ˊˋ.onDismiss();
            } else if (z) {
                ((a) this.ˎ.get(0)).ˏ.ˏ(false);
            }
        }
    }

    public boolean ˏ() {
        return false;
    }

    public Parcelable ʽ() {
        return null;
    }

    public void ˏ(Parcelable parcelable) {
    }

    public void ॱ(int i) {
        if (this.ॱˋ != i) {
            this.ॱˋ = i;
            this.ॱᐝ = hj.ˎ(i, ViewCompat.ʼ(this.ॱˎ));
        }
    }

    public void ˎ(@NonNull View view) {
        if (this.ॱˎ != view) {
            this.ॱˎ = view;
            this.ॱᐝ = hj.ˎ(this.ॱˋ, ViewCompat.ʼ(this.ॱˎ));
        }
    }

    public void ॱ(OnDismissListener onDismissListener) {
        this.ˊˋ = onDismissListener;
    }

    public ListView ᐝ() {
        if (this.ˎ.isEmpty()) {
            return null;
        }
        return ((a) this.ˎ.get(this.ˎ.size() - 1)).ˋ();
    }

    public void ˏ(int i) {
        this.ʻॱ = true;
        this.ʿ = i;
    }

    public void ˎ(int i) {
        this.ʼॱ = true;
        this.ʽॱ = i;
    }

    public void ॱ(boolean z) {
        this.ˈ = z;
    }

    public boolean ʻ() {
        return false;
    }
}
